﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI.WebControls;
using STT.Business;
using System.Web.UI;
using System.Collections;

namespace STT.Admin
{
    public class Methods
    {
        public static void SortDDL(ref DropDownList ddControl)
        {
            ArrayList textList = new ArrayList();
            ArrayList valueList = new ArrayList();


            foreach (ListItem li in ddControl.Items)
            {
                textList.Add(li.Text);
            }

            textList.Sort();


            foreach (object item in textList)
            {
                string value = ddControl.Items.FindByText(item.ToString()).Value;
                valueList.Add(value);
            }
            ddControl.Items.Clear();

            for (int i = 0; i < textList.Count; i++)
            {
                ListItem objItem = new ListItem(textList[i].ToString(), valueList[i].ToString());
                ddControl.Items.Add(objItem);
            }
        }
        public static void TinhTP(ref DropDownList ddControl)
        {
            ddControl.DataSource = BTbTinh_TP.SelectAll1();
            ddControl.DataTextField = "TenTinh_TP";
            ddControl.DataValueField = "MaTinh_TP";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }
        public static void NhomNL(ref DropDownList ddControl)
        {
            ddControl.DataSource = BTbNhomNL.SelectAll();
            ddControl.DataTextField = "TenNhomNL";
            ddControl.DataValueField = "MaNhomNL";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }
        public static void DanToc(ref DropDownList ddControl)
        {
            
            ddControl.DataSource = BTbDanToc.SelectAll1();
            ddControl.DataTextField = "TenDT";
            ddControl.DataValueField = "MaDT";
            ddControl.DataBind();
            //Array.Sort(ddControl.DataTextField.ToArray().OrderBy());
            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }
        public static void MaNgachLuong(ref DropDownList ddControl)
        {
            ddControl.DataSource = BTbMaNgachLuong.SelectAll();
            ddControl.DataTextField = "TenNL";
            ddControl.DataValueField = "MaNL";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }
        public static void MaNgachLuong_LoadDropDownList(ref DropDownList ddControl, int MaNhomNL)
        {
            ddControl.DataSource = BTbMaNgachLuong.LoadDropdownlist(MaNhomNL);
            ddControl.DataTextField = "TenNL";
            ddControl.DataValueField = "MaNL";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }
        public static void HeSoLuong(ref DropDownList ddControl)
        {
            ddControl.DataSource = BTbHeSoLuong.SelectAll();
            ddControl.DataTextField = "HeSL";
            ddControl.DataValueField = "HeSL";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }
        public static void HeSoLuong_LoadDropDownList(ref DropDownList ddControl, int MaNhomNL)
        {
            ddControl.DataSource = BTbHeSoLuong.LoadDropdownlist(MaNhomNL);
            ddControl.DataTextField = "HeSL";
            ddControl.DataValueField = "HeSL";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }
        public static void HuyenQuan(ref DropDownList ddControl)
        {
            ddControl.DataSource = BTbHuyen_Quan.SelectAll1();
            ddControl.DataTextField = "TenHuyen_Quan";
            ddControl.DataValueField = "MaHuyenQuan";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }
        public static void ChucVu(ref DropDownList ddControl)
        {
            //ddControl.Items.Clear();
            ddControl.DataSource = BTbChucVu.SelectAll();
            ddControl.DataTextField = "TenChucVu";
            ddControl.DataValueField = "MaChucVu";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }
        public static void LopHocByMaGV(ref DropDownList ddControl, string MaGV)
        {
            //ddControl.Items.Clear();
            ddControl.DataSource = BTbLop.SelectLopHocByMaGV(MaGV);
            ddControl.DataTextField = "TenLop";
            ddControl.DataValueField = "MaLop";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }
        public static void LopHocByKhoaHoc(ref DropDownList ddControl, string MaKH)
        {
            //ddControl.Items.Clear();
            ddControl.DataSource = BTbLop.SelectByKhoaHoc(MaKH);
            ddControl.DataTextField = "TenLop";
            ddControl.DataValueField = "MaLop";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }

        public static void KhoaHocByDonVi(ref DropDownList ddControl, string donvi)
        {
            ddControl.DataSource = BTbKhoaHoc.SelectAllByDonVi(donvi);
            ddControl.DataTextField = "TenKhoaHoc";
            ddControl.DataValueField = "MaKhoaHoc";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }
        public static void MonHocByMaLopMaGV(ref DropDownList ddControl, string Malop, string Magv)
        {
            ddControl.DataSource = BTbMonHoc.SelectMonHocByLopHocByGiangVien(Malop, Magv);
            ddControl.DataTextField = "TenMonHoc";
            ddControl.DataValueField = "MaMonHoc";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }
        public static void LopHocByKhoaHocID(ref DropDownList ddControl, string Makh, string MaGV)
        {
            //ddControl.Items.Clear();
            ddControl.DataSource = BTbLop.SelectLopHocByKhoaHocMaGV(Makh, MaGV);
            ddControl.DataTextField = "TenLop";
            ddControl.DataValueField = "MaLop";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }
        public static void Chon(ref DropDownList ddControl)
        {


            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }
        public static void Quyen(ref DropDownList ddControl)
        {
            //ddControl.Items.Clear();
            ddControl.DataSource = BTbQuyen.SelectAll2();
            ddControl.DataTextField = "TenQuyen";
            ddControl.DataValueField = "MaQuyen";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }
        public static void ChucNang(ref DropDownList ddControl)
        {
            ddControl.Items.Clear();
            ddControl.DataSource = BTbChucNang.SelectAll();
            ddControl.DataTextField = "TenCN";
            ddControl.DataValueField = "MaCN";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Không---------", Value = "0" });
            
        }
        public static void MonHocByKHGVLOP(ref DropDownList ddControl, string Makh, string Malop, string magv)
        {
            //ddControl.Items.Clear();
            ddControl.DataSource = BTbMonHoc.SelectByGVKHLOP(Makh, Malop, magv);
            ddControl.DataTextField = "TenMonHoc";
            ddControl.DataValueField = "MaMonHoc";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }

        public static void KhoaHoc2(ref DropDownList ddControl, string Magv)
        {
            ddControl.DataSource = BTbKhoaHoc.SelectKhoaHocByMagv(Magv);
            ddControl.DataTextField = "TenKhoaHoc";
            ddControl.DataValueField = "MaKhoaHoc";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }

        public static void DonViHocVien(ref DropDownList ddControl)
        {

            ddControl.DataSource = BTbDonVi.SelectAllDonViHocVien();
            ddControl.DataTextField = "TenDonVi";
            ddControl.DataValueField = "MaDonVi";
            ddControl.DataBind();

            if (ddControl.Items.Count > 0)
            {
                if (ddControl.Items.Count > 1)
                {
                    ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
                }
                ddControl.SelectedIndex = 0;
            }
        }
        public static void LopHocByKhoaHocID(ref DropDownList ddControl, string Makh)
        {
            //ddControl.Items.Clear();
            ddControl.DataSource = BTbLop.SelectByKhoaHoc(Makh);
            ddControl.DataTextField = "TenLop";
            ddControl.DataValueField = "MaLop";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }

        public static void GiangVienSelectAll(ref DropDownList ddControl)
        {
            //ddControl.Items.Clear();
            ddControl.DataSource = BTbGiangVien.SelectAllForDDL();
            ddControl.DataTextField = "TenGV";
            ddControl.DataValueField = "MaGV";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }
        public static void GiangVienSelectAllForCV(ref DropDownList ddControl)
        {
            //ddControl.Items.Clear();
            ddControl.DataSource = BTbGiangVien.SelectAllForCV();
            ddControl.DataTextField = "TenGV";
            ddControl.DataValueField = "MaGV";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }
        public static void DonViGiangVien(ref DropDownList ddControl, string TenDN)
        {
            ddControl.DataSource = BTbDonVi.SelectAllDonViGiangVien(TenDN);
            ddControl.DataTextField = "TenDonVi";
            ddControl.DataValueField = "MaDonVi";
            ddControl.DataBind();
            if (ddControl.Items.Count > 0)
            {
                if (ddControl.Items.Count > 1)
                {
                    ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
                }
                ddControl.SelectedIndex = 0;
            }
        }

        public static void ChuyenMon(ref DropDownList ddControl)
        {
            ddControl.DataSource = BTbTDChuyenMon.SelectAll();
            ddControl.DataTextField = "TenTDChuyenMon";
            ddControl.DataValueField = "MaTDChuyenMon";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }

        public static void NgoaiNgu(ref DropDownList ddControl)
        {  
            ddControl.DataSource = BTbNgoaiNgu.SelectAll();
            ddControl.DataTextField = "TenNgoaiNgu";
            ddControl.DataValueField = "MaNgoaiNgu";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }

        public static void NgoaiNgu(ref CheckBoxList ddControl)
        {
            ddControl.DataSource = BTbNgoaiNgu.SelectAll();
            ddControl.DataTextField = "TenNgoaiNgu";
            ddControl.DataValueField = "MaNgoaiNgu";
            ddControl.DataBind();

        }

      
        public static void MonHoc2(ref DropDownList ddControl)
        {
            ddControl.DataSource = BTbMonHoc.SelectAll2();
            ddControl.DataTextField = "TenMonHoc";
            ddControl.DataValueField = "MaMonHoc";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }
        public static void Nganh(ref DropDownList ddControl)
        {
            ddControl.DataSource = BTbNganh.SelectAll();
            ddControl.DataTextField = "TenNganh";
            ddControl.DataValueField = "MaNganh";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }
        public static void PhongBan(ref DropDownList ddControl)
        {
            ddControl.DataSource = BTbPhongBan.SelectAll();
            ddControl.DataTextField = "TenPB";
            ddControl.DataValueField = "MaPB";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }
        public static void LoaiTinTuc(ref DropDownList ddControl)
        {
            ddControl.DataSource = BTbLoaiTinTuc.SelectAll();
            ddControl.DataTextField = "TenLoaiTT";
            ddControl.DataValueField = "MaLoaiTT";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
            
        }
        public static void Lop(ref DropDownList ddControl)
        {
            ddControl.DataSource = BTbLop.SelectAll();
            ddControl.DataTextField = "TenLop";
            ddControl.DataValueField = "MaLop";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }

        public static void HocVan(ref DropDownList ddControl)
        {
            ddControl.DataSource = BTbTDHocVan.SelectAll();
            ddControl.DataTextField = "TenTDHocVan";
            ddControl.DataValueField = "MaTDHocVan";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }
        public static void KhoaHoc(ref DropDownList ddControl)
        {
            ddControl.DataSource = BTbKhoaHoc.SelectAll();
            ddControl.DataTextField = "TenKhoaHoc";
            ddControl.DataValueField = "MaKhoaHoc";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }
        public static void NamHoc(ref DropDownList ddControl)
        {
            ddControl.DataSource = BTbKhoaHoc.SelectYear();
            ddControl.DataTextField = "Nam";
            ddControl.DataValueField = "Nam";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }


        public static string GenerateRandomPassword(int length)
        {
            string allowedLetterChars = "abcdefghijkmnpqrstuvwxyzABCDEFGHJKLMNPQRSTUVWXYZ";
            string allowedNumberChars = "0123456789";
            char[] chars = new char[length];
            Random rd = new Random();
            bool useLetter = true;
            for (int i = 0; i < length; i++)
            {
                if (useLetter)
                {
                    chars[i] = allowedLetterChars[rd.Next(0, allowedLetterChars.Length)];
                    useLetter = false;
                }
                else
                {
                    chars[i] = allowedNumberChars[rd.Next(0, allowedNumberChars.Length)];
                    useLetter = true;
                }
            }
            return new string(chars);
        }
        public static string CatChuoi(string Chuoi)
        {
            string[] str = Chuoi.Split('.');
            Chuoi = str[0];
            return Chuoi;
        }

        public static void Cap(ref DropDownList ddControl)
        {
            ddControl.DataSource = BTbCap.SelectAll();
            ddControl.DataTextField = "TenCap";
            ddControl.DataValueField = "MaCap";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }
        public static void MonHocByMaLopTKBNamHoc(ref DropDownList ddControl, string Malop)
        {
            ddControl.DataSource = BTbMonHoc.SelectMonHocByLopHocByTKB(Malop);
            ddControl.DataTextField = "TenMonHoc";
            ddControl.DataValueField = "MaMonHoc";
            ddControl.DataBind();

            ddControl.Items.Insert(0, new ListItem { Text = "---------Chọn---------", Value = "-1" });
            ddControl.SelectedIndex = 0;
        }
    }
}